package com.like.bluetooth.sample.classic

import android.annotation.SuppressLint
import androidx.recyclerview.widget.DiffUtil
import com.like.bluetooth.sample.databinding.ItemClassicScanBinding
import com.like.recyclerview.adapter.BaseListAdapter
import com.like.recyclerview.viewholder.BindingViewHolder

class ClassicScanAdapter(private val onGotoConnect: (ClassicScanInfo) -> Unit) :
    BaseListAdapter<ItemClassicScanBinding, ClassicScanInfo>(DIFF) {

    override fun onBindViewHolder(holder: BindingViewHolder<ItemClassicScanBinding>, item: ClassicScanInfo?) {
        super.onBindViewHolder(holder, item)
        item ?: return
        holder.binding.tvConnect.setOnClickListener {
            onGotoConnect(item)
        }
    }

    companion object {
        private val DIFF = object : DiffUtil.ItemCallback<ClassicScanInfo>() {
            override fun areItemsTheSame(oldItem: ClassicScanInfo, newItem: ClassicScanInfo): Boolean {
                return oldItem.address == newItem.address
            }

            @SuppressLint("DiffUtilEquals")
            override fun areContentsTheSame(oldItem: ClassicScanInfo, newItem: ClassicScanInfo): Boolean {
                return oldItem.name == newItem.name &&
                        oldItem.rssi == newItem.rssi &&
                        oldItem.distance == newItem.distance
            }
        }
    }
}